/*
 * Copyright (c) 2018. 玺得（深圳）科技有限公司.保留所有权利. http://www.pi-top.com/ 玺得（深圳）科技有限公司保留所有代码著作权.如有任何疑问请访问官方网站与我们联系. 代码只针对特定需求定制编码，不得在未经允许或授权的情况下对外传播扩散.恶意传播者，法律后果自行承担. 本代码仅用于jv-rover系统.
 */
package com.pitop.device.service;

import com.pitop.core.entity.Page;
import com.pitop.core.exceptions.BaseException;
import com.pitop.core.exceptions.DeviceOnlineException;
import com.pitop.device.dao.DeviceOnlineDAO;
import com.pitop.device.entity.DeviceOnlineState;
import com.pitop.device.entity.DeviceOnline;
import com.pitop.core.base.BaseDAO;
import com.pitop.core.base.BaseSVImpl;
import com.alibaba.fastjson.serializer.SerializerFeature;
import lombok.extern.slf4j.Slf4j;
import com.alibaba.fastjson.JSON;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;
import java.util.HashMap;
import java.util.Date;
import java.util.Map;

import org.apache.ibatis.session.RowBounds;
import com.baidu.fsg.uid.UidGenerator;

@Service("deviceOnlineSV")
@Slf4j
public class DeviceOnlineSVImpl extends BaseSVImpl<DeviceOnline, Long> implements DeviceOnlineSV {

    @Autowired
    private DeviceOnlineDAO deviceOnlineDAO;

    @Resource
    private UidGenerator uidGenerator;

    @Override
    protected BaseDAO getBaseDAO() {
        return deviceOnlineDAO;
    }

    /**
     * 保存account对象
     *
     * @param entity 实体
     * @throws BaseException
     */
    @Override
    public void save(DeviceOnline entity) throws BaseException {
        super.save(entity);
    }

    /**
     * 加载一个对象DeviceOnline
     *
     * @param id * @param groupCode 组编码* @param groupName 组名称* @param deviceCode 设备编码* @param deviceCodeName 设备代码
     * @return DeviceOnline
     */
    @Override
    public DeviceOnline load(java.lang.Long id, java.lang.String groupCode, java.lang.String groupName, java.lang.String deviceCode, java.lang.String deviceCodeName) {
        if (id == null && groupCode == null && groupName == null && deviceCode == null && deviceCodeName == null) {
            throw new DeviceOnlineException(BaseException.BaseExceptionEnum.Ilegal_Param);
        }

        Map<String, Object> param = new HashMap<>();
        param.put("id", id);
        param.put("groupCode", groupCode);
        param.put("groupName", groupName);
        param.put("deviceCode", deviceCode);
        param.put("deviceCodeName", deviceCodeName);
        return deviceOnlineDAO.load(param);
    }

    /**
     * 加载一个对象DeviceOnline 通过id
     *
     * @param id
     * @return DeviceOnline
     */
    @Override
    public DeviceOnline loadById(java.lang.Long id) {
        if (id == null) {
            throw new DeviceOnlineException(BaseException.BaseExceptionEnum.Ilegal_Param);
        }
        return deviceOnlineDAO.loadById(id);
    }

    /**
     * 加载一个对象DeviceOnline 通过groupCode
     *
     * @param groupCode 组编码
     * @return DeviceOnline
     */
    @Override
    public DeviceOnline loadByGroupCode(java.lang.String groupCode) {
        if (groupCode == null) {
            throw new DeviceOnlineException(BaseException.BaseExceptionEnum.Ilegal_Param);
        }
        return deviceOnlineDAO.loadByGroupCode(groupCode);
    }

    /**
     * 加载一个对象DeviceOnline 通过groupName
     *
     * @param groupName 组名称
     * @return DeviceOnline
     */
    @Override
    public DeviceOnline loadByGroupName(java.lang.String groupName) {
        if (groupName == null) {
            throw new DeviceOnlineException(BaseException.BaseExceptionEnum.Ilegal_Param);
        }
        return deviceOnlineDAO.loadByGroupName(groupName);
    }

    /**
     * 加载一个对象DeviceOnline 通过deviceCode
     *
     * @param deviceCode 设备编码
     * @return DeviceOnline
     */
    @Override
    public DeviceOnline loadByDeviceCode(java.lang.String deviceCode) {
        if (deviceCode == null) {
            throw new DeviceOnlineException(BaseException.BaseExceptionEnum.Ilegal_Param);
        }
        return deviceOnlineDAO.loadByDeviceCode(deviceCode);
    }

    /**
     * 加载一个对象DeviceOnline 通过deviceCodeName
     *
     * @param deviceCodeName 设备代码
     * @return DeviceOnline
     */
    @Override
    public DeviceOnline loadByDeviceCodeName(java.lang.String deviceCodeName) {
        if (deviceCodeName == null) {
            throw new DeviceOnlineException(BaseException.BaseExceptionEnum.Ilegal_Param);
        }
        return deviceOnlineDAO.loadByDeviceCodeName(deviceCodeName);
    }


    /**
     * 根据主键id,oldStates 共同更新 DeviceOnline 的状态到newState状态
     *
     * @param id
     * @param newState  新状态
     * @param oldStates 旧状态集合
     */
    @Override
    public void updateStateById(java.lang.Long id, DeviceOnlineState newState, DeviceOnlineState... oldStates) {
        if (id == null) {
            throw new DeviceOnlineException(BaseException.BaseExceptionEnum.Ilegal_Param);
        }
        if (newState == null) {
            throw new DeviceOnlineException(BaseException.BaseExceptionEnum.Ilegal_Param);
        }
        if (oldStates == null) {
            throw new DeviceOnlineException(BaseException.BaseExceptionEnum.Ilegal_Param);
        }
        deviceOnlineDAO.updateStateById(id, new Date(), newState, oldStates);
    }

    /**
     * 根据主键groupCode,oldStates 共同更新 DeviceOnline 的状态到newState状态
     *
     * @param groupCode 组编码
     * @param newState  新状态
     * @param oldStates 旧状态集合
     */
    @Override
    public void updateStateByGroupCode(java.lang.String groupCode, DeviceOnlineState newState, DeviceOnlineState... oldStates) {
        if (groupCode == null) {
            throw new DeviceOnlineException(BaseException.BaseExceptionEnum.Ilegal_Param);
        }
        if (newState == null) {
            throw new DeviceOnlineException(BaseException.BaseExceptionEnum.Ilegal_Param);
        }
        if (oldStates == null) {
            throw new DeviceOnlineException(BaseException.BaseExceptionEnum.Ilegal_Param);
        }
        deviceOnlineDAO.updateStateByGroupCode(groupCode, new Date(), newState, oldStates);
    }

    /**
     * 根据主键groupName,oldStates 共同更新 DeviceOnline 的状态到newState状态
     *
     * @param groupName 组名称
     * @param newState  新状态
     * @param oldStates 旧状态集合
     */
    @Override
    public void updateStateByGroupName(java.lang.String groupName, DeviceOnlineState newState, DeviceOnlineState... oldStates) {
        if (groupName == null) {
            throw new DeviceOnlineException(BaseException.BaseExceptionEnum.Ilegal_Param);
        }
        if (newState == null) {
            throw new DeviceOnlineException(BaseException.BaseExceptionEnum.Ilegal_Param);
        }
        if (oldStates == null) {
            throw new DeviceOnlineException(BaseException.BaseExceptionEnum.Ilegal_Param);
        }
        deviceOnlineDAO.updateStateByGroupName(groupName, new Date(), newState, oldStates);
    }

    /**
     * 根据主键deviceCode,oldStates 共同更新 DeviceOnline 的状态到newState状态
     *
     * @param deviceCode 设备编码
     * @param newState   新状态
     * @param oldStates  旧状态集合
     */
    @Override
    public void updateStateByDeviceCode(java.lang.String deviceCode, DeviceOnlineState newState, DeviceOnlineState... oldStates) {
        if (deviceCode == null) {
            throw new DeviceOnlineException(BaseException.BaseExceptionEnum.Ilegal_Param);
        }
        if (newState == null) {
            throw new DeviceOnlineException(BaseException.BaseExceptionEnum.Ilegal_Param);
        }
        if (oldStates == null) {
            throw new DeviceOnlineException(BaseException.BaseExceptionEnum.Ilegal_Param);
        }
        deviceOnlineDAO.updateStateByDeviceCode(deviceCode, new Date(), newState, oldStates);
    }

    /**
     * 根据主键deviceCodeName,oldStates 共同更新 DeviceOnline 的状态到newState状态
     *
     * @param deviceCodeName 设备代码
     * @param newState       新状态
     * @param oldStates      旧状态集合
     */
    @Override
    public void updateStateByDeviceCodeName(java.lang.String deviceCodeName, DeviceOnlineState newState, DeviceOnlineState... oldStates) {
        if (deviceCodeName == null) {
            throw new DeviceOnlineException(BaseException.BaseExceptionEnum.Ilegal_Param);
        }
        if (newState == null) {
            throw new DeviceOnlineException(BaseException.BaseExceptionEnum.Ilegal_Param);
        }
        if (oldStates == null) {
            throw new DeviceOnlineException(BaseException.BaseExceptionEnum.Ilegal_Param);
        }
        deviceOnlineDAO.updateStateByDeviceCodeName(deviceCodeName, new Date(), newState, oldStates);
    }

    /**
     * 根据主键id 更新 DeviceOnline 的状态到另一个状态
     *
     * @param id
     * @param state 状态
     */
    @Override
    public void updateById(java.lang.Long id, DeviceOnlineState state) {
        if (id == null) {
            throw new DeviceOnlineException(BaseException.BaseExceptionEnum.Ilegal_Param);
        }
        deviceOnlineDAO.updateById(id, state, new Date());
    }

    /**
     * 根据主键groupCode 更新 DeviceOnline 的状态到另一个状态
     *
     * @param groupCode 组编码
     * @param state     状态
     */
    @Override
    public void updateByGroupCode(java.lang.String groupCode, DeviceOnlineState state) {
        if (groupCode == null) {
            throw new DeviceOnlineException(BaseException.BaseExceptionEnum.Ilegal_Param);
        }
        deviceOnlineDAO.updateByGroupCode(groupCode, state, new Date());
    }

    /**
     * 根据主键groupName 更新 DeviceOnline 的状态到另一个状态
     *
     * @param groupName 组名称
     * @param state     状态
     */
    @Override
    public void updateByGroupName(java.lang.String groupName, DeviceOnlineState state) {
        if (groupName == null) {
            throw new DeviceOnlineException(BaseException.BaseExceptionEnum.Ilegal_Param);
        }
        deviceOnlineDAO.updateByGroupName(groupName, state, new Date());
    }

    /**
     * 根据主键deviceCode 更新 DeviceOnline 的状态到另一个状态
     *
     * @param deviceCode 设备编码
     * @param state      状态
     */
    @Override
    public void updateByDeviceCode(java.lang.String deviceCode, DeviceOnlineState state) {
        if (deviceCode == null) {
            throw new DeviceOnlineException(BaseException.BaseExceptionEnum.Ilegal_Param);
        }
        deviceOnlineDAO.updateByDeviceCode(deviceCode, state, new Date());
    }

    /**
     * 根据主键deviceCodeName 更新 DeviceOnline 的状态到另一个状态
     *
     * @param deviceCodeName 设备代码
     * @param state          状态
     */
    @Override
    public void updateByDeviceCodeName(java.lang.String deviceCodeName, DeviceOnlineState state) {
        if (deviceCodeName == null) {
            throw new DeviceOnlineException(BaseException.BaseExceptionEnum.Ilegal_Param);
        }
        deviceOnlineDAO.updateByDeviceCodeName(deviceCodeName, state, new Date());
    }


    /**
     * 删除对象DeviceOnline
     *
     * @param id * @param groupCode 组编码* @param groupName 组名称* @param deviceCode 设备编码* @param deviceCodeName 设备代码
     * @return DeviceOnline
     */
    @Override
    public void delete(java.lang.Long id, java.lang.String groupCode, java.lang.String groupName, java.lang.String deviceCode, java.lang.String deviceCodeName) {
        if (id == null && groupCode == null && groupName == null && deviceCode == null && deviceCodeName == null) {
            throw new DeviceOnlineException(BaseException.BaseExceptionEnum.Ilegal_Param);
        }
        Map<String, Object> param = new HashMap<>();
        param.put("id", id);
        param.put("groupCode", groupCode);
        param.put("groupName", groupName);
        param.put("deviceCode", deviceCode);
        param.put("deviceCodeName", deviceCodeName);
        deviceOnlineDAO.delete(param);
    }


    /**
     * 查询DeviceOnline分页
     *
     * @param deviceOnline 对象
     * @param offset       查询开始行
     * @param limit        查询行数
     * @return List<DeviceOnline>
     */
    @Override
    public List<DeviceOnline> list(DeviceOnline deviceOnline, int offset, int limit) {
        if (offset < 0) {
            offset = 0;
        }

        if (limit < 0) {
            limit = Page.limit;
        }

        Map<String, Object> map = null;
        if (deviceOnline != null) {
            map = JSON.parseObject(JSON.toJSONString(deviceOnline, SerializerFeature.WriteDateUseDateFormat));
        } else {
            map = new HashMap<>();
        }
        return deviceOnlineDAO.list(map, new RowBounds(offset, limit));
    }

    @Override
    public int count(DeviceOnline deviceOnline) {
        Map<String, Object> map = null;
        if (deviceOnline != null) {
            map = JSON.parseObject(JSON.toJSONString(deviceOnline, SerializerFeature.WriteDateUseDateFormat));
        } else {
            map = new HashMap<>();
        }
        return deviceOnlineDAO.count(map);
    }

    /**
     * 查询DeviceOnline分页
     *
     * @param id     * @param groupCode  组编码* @param groupName  组名称* @param deviceCode  设备编码* @param deviceCodeName  设备代码
     * @param offset 查询开始行
     * @param limit  查询行数
     * @return List<DeviceOnline>
     */
    @Override
    public List<DeviceOnline> list(java.lang.Long id, java.lang.String groupCode, java.lang.String groupName, java.lang.String deviceCode, java.lang.String deviceCodeName, int offset, int limit) {
        if (offset < 0) {
            offset = 0;
        }

        if (limit < 0) {
            limit = Page.limit;
        }
        Map<String, Object> param = new HashMap<>();
        param.put("id", id);
        param.put("groupCode", groupCode);
        param.put("groupName", groupName);
        param.put("deviceCode", deviceCode);
        param.put("deviceCodeName", deviceCodeName);
        return deviceOnlineDAO.list(param, new RowBounds(offset, limit));
    }

    @Override
    public int count(java.lang.Long id, java.lang.String groupCode, java.lang.String groupName, java.lang.String deviceCode, java.lang.String deviceCodeName) {
        Map<String, Object> param = new HashMap<>();
        param.put("id", id);
        param.put("groupCode", groupCode);
        param.put("groupName", groupName);
        param.put("deviceCode", deviceCode);
        param.put("deviceCodeName", deviceCodeName);
        return deviceOnlineDAO.count(param);
    }

}
